package com.ytjj.qmyx.supplychain.common.utils.feishu;

import com.alibaba.fastjson.JSON;
import com.ytjj.qmyx.supplychain.common.utils.HttpClientUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 飞书异常监控
 */

@Slf4j
@Service
public class FeiShuUtil {

    @Getter
    public static String URL;

    @Getter
    public static String UrlMail;

    @Value("${feiShuUrl:}")
    public void setUrl(String feiShuUrl) {
        URL = feiShuUrl;
    }

    @Value("${feiShuUrlMail:}")
    public void setUrlMail(String feiShuUrlMail) {
        UrlMail = feiShuUrlMail;
    }

    /**
     * 发送报警消息
     * @param msg
     */
    public static void sendMsg(String msg){
        try {
            if (StringUtils.isBlank(msg) || StringUtils.isBlank(URL)){
                return;
            }
            Map<String,Object> map = new HashMap<>();
            map.put("msg_type","text");
            map.put("content",new FeishuEntity(msg));
            String jsonStr = JSON.toJSONString(map);
            HttpClientUtil.doPostJson(URL,jsonStr);
        } catch (Exception e) {
            log.error("=================发送飞书异常消息出错,{}",e.getMessage());
        }
    }

    /**
     * 发送报警消息(邮件通知相关)
     * @param msg
     */
    public static void sendMsgForEmail(String msg){
        try {
            if (StringUtils.isBlank(msg) || StringUtils.isBlank(UrlMail)){
                return;
            }
            Map<String,Object> map = new HashMap<>();
            map.put("msg_type","text");
            map.put("content",new FeishuEntity(msg));
            String jsonStr = JSON.toJSONString(map);
            HttpClientUtil.doPostJson(UrlMail,jsonStr);
        } catch (Exception e) {
            log.error("=================发送飞书异常消息出错,{}",e.getMessage());
        }
    }

    public static void main(String[] args) {
        sendMsg("测试报警,不用管");
    }
}
